package com.share.notification.jconsole;

import javax.management.Notification;
import javax.management.NotificationBroadcasterSupport;

/**
 * 能够主动发送"通知"的MBean.
 * 继承: NotificationBroadcasterSupport
 *
 */
public class NotifyHello extends NotificationBroadcasterSupport implements NotifyHelloMBean {
	private String name;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public void printHello() {
		System.out.println("Hello , " + name);
	}

	public String queryStatusAndSendNotification() {
		String rs = this.name + " : status = Good";
		System.out.println(rs);
		//重点: 给监听者 发送通知
		sendNotificationToListener(rs);
		return  rs;
	}

	/**
	 * 发送通知. 使用 NotificationBroadcasterSupport.sendNotification() 方法
	 * Notification :消息内容.
	 * @param message
	 */
	private void sendNotificationToListener(String message){
		int sequenceNo = 1;
		Notification notification = new Notification("Info-to-Client", this,
				sequenceNo, message);
		this.sendNotification(notification);
	}
}
